package it.unibo.android.convoycruisecontrol.boatsubsystem;

import java.text.NumberFormat;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import it.unibo.convoycruisecontrol.boatsubsystem.IDashboard;
import it.unibo.convoycruisecontrol.boatsubsystem.ObservableDashboard;
import it.unibo.convoycruisecontrol.progettodesignandroid.R;
import it.unibo.convoycruisecontrol.progettodesignandroid.R.id;
import it.unibo.convoycruisecontrol.progettodesignandroid.R.layout;
import it.unibo.location.ILocation;
import it.unibo.util.measures.MetersSecondToKilometersSecond;
import it.unibo.util.measures.MetersSecondToKnots;
import it.unibo.util.measures.MetersToKilometers;
import it.unibo.util.measures.MetersToNauticalMiles;

public class DashboardView extends LinearLayout implements Observer {

	private ObservableDashboard dashboard = null;
	private TextView informations = null;
	private TextView positions = null;
	private Activity activity;

	public DashboardView(Activity activity, ObservableDashboard dashboard) {
		super(activity, null);
		this.activity = activity;

		initGui(activity);

		this.dashboard = dashboard;
		dashboard.addObserver(this);
	}

	private void initGui(Activity activity) {
		LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.dashboard, this);
		informations = (TextView) findViewById(R.id.informations);
		positions = (TextView) findViewById(R.id.positions);
	}

	@Override
	public void update(Observable arg0, Object arg1) 
	{
		activity.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				informations.setText(createInformationsString(dashboard.getDisplayedCurrentSpeed(),dashboard.getDisplayedDistanceCovered()));
				positions.setText(createPositionsString(dashboard.getDisplayedLocations()));
			}
		});
	}

	private String createInformationsString(float speed, float distance)
	{
		NumberFormat formatter = NumberFormat.getInstance();
		formatter.setMaximumFractionDigits(1);
		StringBuilder builder = new StringBuilder();
		builder.append("Current Speed:\n");
		builder.append("\t in kn:");
		builder.append(formatter.format(MetersSecondToKnots.convert(speed)));
		builder.append("\t in km/sec:");
		formatter.setMaximumFractionDigits(3);
		builder.append(formatter.format(MetersSecondToKilometersSecond.convert(speed)));
		builder.append("\n\n");
		builder.append("Distance covered:\n");
		builder.append("\t in km:");
		builder.append(formatter.format(MetersToKilometers.convert(distance)));
		builder.append("\t in Nm:");
		builder.append(formatter.format(MetersToNauticalMiles.convert(distance)));

		return builder.toString();
	}
	
	private String createPositionsString(List<ILocation> locations) {
		StringBuilder builder = new StringBuilder();
		builder.append("Positions\n");
		for (int i = 0; i < locations.size(); i++) {
			builder.append("\nBoat " + i + ": ");
			builder.append(locations.get(i).toString());
		}

		return builder.toString();
	}

}
